Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

12.2 Foundational Principles of Optimizing Program Performance via Label Modification

The jump label modification technique primarily utilizes two central data structures—the jump entry (struct jump_entry) and the static key (struct static_key)—to perform code patching. These two data structures are defined in the file include/linux/jump_label.h as follows:

struct jump_entry {
	jump_label_t code;	
	jump_label_t target;
	jump_label_t key;	
};
struct static_key {
	atomic_t enabled;
	union {
		unsigned long type;
		struct jump_entry *entries;
		struct static_key_mod *next;
	};
};

Within struct jump_entry, the code field stores the address of the instruction that needs to be modified, while the target field stores the destination address of the branch instruction. The key field, after its lowest two bits are cleared, yields the base memory address of the static_key structure corresponding to this jump entry. The least significant bit (b0) of the key field serves as the branch flag; a value of 0 indicates a low branch probability (referred to as unlikely in Linux terminology), while a value of 1 indicates a high branch probability (referred to as likely in Linux terminology).

The static_key structure contains two primary fields. The first is enabled, and the second is a multi-purpose field of type unsigned long shared within a union. Bit b0 of this shared field represents the type bit, where 0 indicates that the key is initialized to false and 1 indicates that the key is initialized to true. Bit b1 is utilized to distinguish whether this entry stores the address of a jump_entry or a static_key_mod; 0 indicates it points to a jump_entry address, and 1 indicates it points to a static_key_mod address. The type bit is leveraged during the compilation phase to emit code, whereas the enabled field is used at runtime to dynamically toggle the routing direction of the branch instruction.

The precise relationships among the fields of jump_entry, static_key, and the executable program code are illustrated in Figure 12-2.

Figure 12-2 Relationships among static_key, jump_entry, and Program Code

During the compilation phase, the compiler performs an XOR operation between the type bit and the branch bit to generate the initial instruction according to the matrix below:

Table 12-1 Determination of Patch Instructions by type and branch

type \ branchlikelyunlikely
trueNOPjump
falsejumpNOP

If the XOR product of type and branch evaluates to 0, a NOP instruction is generated. If the XOR product of type and branch evaluates to 1, an unconditional jump instruction is generated.

At compile time, relying purely on the type and branch bits only emits either a NOP or an unconditional jump instruction, meaning the execution path remains static and only one section (either the likely or unlikely path) will be executed. To dynamically toggle the branching path at runtime, the kernel must leverage the value of the enabled field to hot-patch the code inline while the program is running.

When code patching is triggered, an XOR operation is performed between the enabled field of the static key and the branch bit. The patch instruction type is determined based on Table 12-2, and an appropriate patch instruction is subsequently generated. If the resolved patch type is a NOP, the memory address pointed to by the code field of the jump entry is overwritten with a hardware NOP instruction. If the resolved patch type is a branch instruction, the system calculates the relative jump displacement using the instruction address in the code field (which typically acts as the Program Counter reference value) and the destination address in the target field. This computed branch instruction is then written directly into the memory location designated by code, thereby successfully patching the instruction. This type of patching work is commonly executed during the system boot phase.

Table 12-2 Determination of Patch Instructions by enabled and branch

enabled \branchtrue(likely)false(unlikely)
trueNOPjump
falsejumpNOP
Copyright © 2026 Guangzhou eSmart Information Technology Co., Ltd. All Rights Reserved.
All content in this book is copyrighted by the author. For personal learning and exchange only. Commercial use without authorization is strictly prohibited. Email:

✉ 3348194517@qq.com | hai616878@gmail.com